#include "textbubble.h"
#include<QFont>
#include<QFontMetricsF>
#include<QTextDocument>
#include<QTextBlock>
#include<QRectF>
TextBubble::TextBubble(ChatRole role, const QString &text, QWidget *parent)
    :BubbleFrame(role,parent)
{
    m_pTextEdit=new QTextEdit();
    m_pTextEdit->setReadOnly(true);
    m_pTextEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pTextEdit->installEventFilter(this);
    QFont font("Microsoft YaHei");
    font.setPointSize(12);
    m_pTextEdit->setFont(font);
    setPlainText(text);
    setWidget(m_pTextEdit);
    initStyleSheet();
}

bool TextBubble::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==m_pTextEdit&&event->type()==QEvent::Paint){
        adjustTextHeight();
    }
    return BubbleFrame::eventFilter(watched,event);
}

void TextBubble::adjustTextHeight()
{
    qreal doc_margin=m_pTextEdit->document()->documentMargin();
    QTextDocument *doc=m_pTextEdit->document();
    qreal text_height=0;
    for(QTextBlock it=doc->begin();it!=doc->end();it=it.next()){
        QTextLayout *pLayout=it.layout();
        QRectF rectf=pLayout->boundingRect();
        text_height+=rectf.height();
    }
    int vMargin=this->layout()->contentsMargins().top();
    setFixedHeight(text_height+doc_margin*2+vMargin*2);

}

void TextBubble::setPlainText(const QString &text)
{
    m_pTextEdit->setPlainText(text);
    //找到段落中最大宽度
    qreal doc_margin=m_pTextEdit->document()->documentMargin();
    int margin_left=this->layout()->contentsMargins().left();
    int margin_right=this->layout()->contentsMargins().right();
    QFontMetricsF fm(m_pTextEdit->font());
    QTextDocument *doc=m_pTextEdit->document();
    int max_width=0;
    //遍历每一段，找到最宽的那一段
    for(QTextBlock it=doc->begin();it!=doc->end();it=it.next()){
        int txtW=int(fm.width(it.text()));
        max_width=max_width<txtW?txtW:max_width;
    }
    //设置该气泡最大宽度
    setMaximumWidth(max_width+doc_margin*2+(margin_left+margin_right));
}

void TextBubble::initStyleSheet()
{
    m_pTextEdit->setStyleSheet("QTextEdit{background:transparent;border:none}");
}
